package org.bonorsoft.modules.control.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.constant.CommonConstant;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Building;
import org.bonorsoft.modules.control.entity.House;
import org.bonorsoft.modules.control.entity.TreeData;
import org.bonorsoft.modules.control.entity.Unit;
import org.bonorsoft.modules.control.service.*;
import org.bonorsoft.modules.control.vo.Otree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/control/infor")
public class InforController {
    private String prefix = "control/infor";

    @Autowired
    private IInforService inforService;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IResidentialService residentialService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IUnitService unitService;

    /**
     *首页
     */
    @RequiresPermissions("control:infor:view")
    @GetMapping()
    public String index()
    {
        return prefix + "/infor";
    }

    @GetMapping("/treeData")
    @ResponseBody
    public List<Otree> treeData(HttpServletRequest req)
    {
        List<Otree> ztrees = inforService.selectTreeData(req,new TreeData().setDeptName(CommonConstant.OFFICE_KEY));
        return ztrees;
    }

    @GetMapping("/getLatitude/{id}")
    @ResponseBody
    public Result<?> getLatitude(@PathVariable("id") String id){
        String type = addressService.getById(id).getDzlx();
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        if (oConvertUtils.isNotEmpty(type)){

            if ("JZW".equals(type)){
                obj.put("detail", buildingService.getById(id));
            }
            if ("FW".equals(type)){
                obj.put("detail", residentialService.getById(id));
            }

            //判断是否存在实有房屋
            int build_count = houseService.count(new QueryWrapper<House>().eq("dzbm", id));

            //如果不存在房屋则继续执行
            int unit_count = 0;
            if(build_count == 0){
                //判断是否存在实有单位
                unit_count = unitService.count(new QueryWrapper<Unit>().eq("dwdz_dzbm", id));
            }

            if(build_count == 0 && unit_count == 0){
                //即不存在实有单位又不存在实有房屋
                obj.put("build", false);
                obj.put("unit", false);
                result.setResult(obj);
                result.success("查询成功！");
                return result;
            }

            if(build_count > 0){
                obj.put("build", true);
                obj.put("unit", false);
                result.setResult(obj);
                result.success("查询成功！");
                return result;
            }

            if(unit_count > 0){
                obj.put("build", false);
                obj.put("unit", true);
                result.setResult(obj);
                result.success("查询成功！");
                return result;
            }
        }
        return Result.error("数据查询失败!");
    }
}
